package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int addBed(BedDto bedDto) {


        /*try {
            Bed bed = new Bed();
            BeanUtil.copyProperties(bedDto,bed);
            //默认床位状态为未入住0
            bed.setBedStatus(0);

            return bedMapper.insertBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }*/


        //重名判断
        Bed bed = new Bed();
        BeanUtil.copyProperties(bedDto, bed);
        //默认床位状态为未入住0
        bed.setBedStatus(0);
        int num=bedMapper.countByBedNumber(bed.getBedNumber());
        if(num>0){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLICATE_BED_NUMBER);
        }
        return bedMapper.insertBed(bed);

    }

    @Override
    public int deleteById(Long id) {
        Bed bed = bedMapper.selectById(id);
        if (bed.getBedStatus() == 1) {
            throw new BaseException(BasicEnum.BED_DELETE_FAIL);
        }
        return bedMapper.deleteById(id);


    }

    @Override
    public BedVo getBed(Long id) {
        Bed bed = bedMapper.selectById(id);
        return BeanUtil.copyProperties(bed, BedVo.class);

    }

    @Override
    public int updateBed(BedDto bedDto) {
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
        int num=bedMapper.countByBedNumber(bed.getBedNumber());
        if(num>0){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLICATE_BED_NUMBER);
        }
        return bedMapper.updateBed(bed);
    }

}

